package com.ruanko.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ruanko.entity.UserInfoBean;
import com.ruanko.service.UserInfoService;
import com.ruanko.service.impl.UserInfoServiceImpl;

public class UserInfoAction extends ActionSupport {

	private UserInfoBean userInfoBean;
	private List<UserInfoBean> userInfoList;
	private HttpServletRequest request = ServletActionContext.getRequest();
	private UserInfoService userInfoService;
	private String type; // 类型
	private String operate; // submit值
	private String check; // checkbox值
	private int pageNow = 1 ; //初始化为1,默认从第一页开始显示  
    private int pageSize = 5 ; //每页显示5条记录  
    private int pageNum=0;	//信息显示的总页数

	public UserInfoAction() {
		this.userInfoService = new UserInfoServiceImpl();
	}

	/**
	 * 登录
	 * 
	 * @return
	 */
	public String login() {

		boolean b = userInfoService.login(userInfoBean);
		if (b == true) {
			HttpSession session = request.getSession(true);
			session.setAttribute("userid", userInfoBean.getUserId());
			session.setAttribute("userpwd", userInfoBean.getPwd());
			return SUCCESS;
		} else
			return ERROR;
	}
	/**
	 * 注册
	 * 
	 * @return
	 */

	public String register() {

		boolean b = userInfoService.register(userInfoBean);
		if (b == true)
			return SUCCESS;
		else
			return ERROR;
	}
	/**
	 * 添加用户信息
	 * 
	 * @return
	 */
	public String addUser() {

		boolean b = userInfoService.register(userInfoBean);
		if (b == true)
			return SUCCESS;
		else
			return ERROR;
	}
	/**
	 * 更新或删除操作
	 * 
	 * @return
	 */
	public String operates() {

		try {
			String ids = this.getCheck();

			String[] arraycheckbox = ids.split(", ");
			ArrayList<UserInfoBean> operateUserList = new ArrayList<UserInfoBean>();

			for (int i = 0; i < arraycheckbox.length; i++) {
				UserInfoBean userInfo = new UserInfoBean();
				userInfo.setUserId(arraycheckbox[i]);

				if (this.getOperate().equals("修改")
						&& !userInfoList.isEmpty()) {
					for (int j = 0; j < userInfoList.size(); j++) {
						if (userInfoList.get(j).getUserId()
								.equals(arraycheckbox[i])) {
							userInfo.setPwd(userInfoList.get(j).getPwd());
							userInfo.setEmail(userInfoList.get(j).getEmail());
						}
					}
				}
				operateUserList.add(userInfo);
			}
			if (this.getOperate().equals("删除")) {
				if (!operateUserList.isEmpty())
					return deleteUserInfo(operateUserList);// 批量删除用户信息
				else
					return SUCCESS;
			} else if (this.getOperate().equals("修改")) {
				if (!operateUserList.isEmpty())
					return updateUserInfo(operateUserList);// 批量更新用户信息
				else
					return SUCCESS;
			} else {
				return ERROR;
			}
		} catch (Exception e) {
			return SUCCESS;
		}

	}

	/**
	 * 批量删除用户信息
	 * 
	 * @return
	 */
	public String deleteUserInfo(List<UserInfoBean> deleteInfoList) {

		boolean b = userInfoService.deleteUserInfo(deleteInfoList);
		if (b == true)
			return SUCCESS;
		else
			return ERROR;
	}

	/**
	 * 批量更新用户信息
	 * 
	 * @return
	 */
	public String updateUserInfo(List<UserInfoBean> updateInfoList) {

		boolean b = userInfoService.updateUserInfo(updateInfoList);
		if (b == true)
			return SUCCESS;
		else
			return ERROR;
	}

	/**
	 * 查找分页显示的用户数据
	 * 
	 * @return
	 */
	public String getAllUserInfo(){
		int number=userInfoService.getUserInfoList().size();
		if(number%pageSize==0){
			pageNum=number/pageSize;
		}else{
			pageNum=(number/pageSize)+1;
		}
		try {
			userInfoList = userInfoService.getUserInfoByPage(pageSize, pageNow);	
			return SUCCESS;
		} catch (Exception e) {
			return ERROR;
		}
	}
	
	public UserInfoBean getUserInfoBean() {
		return userInfoBean;
	}

	public void setUserInfoBean(UserInfoBean userInfoBean) {
		this.userInfoBean = userInfoBean;
	}

	public List<UserInfoBean> getUserInfoList() {
		return userInfoList;
	}

	public void setUserInfoList(List<UserInfoBean> userInfoList) {
		this.userInfoList = userInfoList;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}

	public String getCheck() {
		return check;
	}

	public void setCheck(String check) {
		this.check = check;
	}

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	
}
